package com.onlineMIS.ORM.entity.chainS.report.rptTemplate;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.onlineMIS.common.Common_util;
import com.onlineMIS.common.Excel2007Template;

import com.onlineMIS.common.loggerLocal;

import com.onlineMIS.ORM.entity.headQ.barcodeGentor.Year;
import com.onlineMIS.ORM.entity.chainS.report.ChainPurchaseStatisReportItem;
import com.onlineMIS.ORM.entity.chainS.report.ChainSalesStatisReportItem;
import com.onlineMIS.ORM.entity.chainS.user.ChainStore;
import com.onlineMIS.ORM.entity.chainS.user.ChainUserInfor;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.Brand;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.Color;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.Product;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.ProductBarcode;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.Quarter;

public class ChainPurchaseStatisticsDetailReportTemplate  extends Excel2007Template{
	private static final String TEMPLATE_NAME = "ChainPurchaseStatisticsReportTemplate2007.xlsx";
	private List<ChainPurchaseStatisReportItem> items = new ArrayList<ChainPurchaseStatisReportItem>();

	private int data_row = 4;
	private final int DATE_COLUMN = 0;
	private final int CHAIN_COLUMN = 1;
	private final int BARCODE_COLUMN = 2;
	private final int PRODUCT_CODE_COLUMN = 3;
	private final int COLOR_COLUMN = 4;
	private final int BRAND_COLUMN = 5;
	private final int QUARTER_COLUMN = 6;
	private final int CATEGORY_LEVEL_COLUMN =7;
	private final int CATEGORY_COLUMN =8;	
	private final int PURCHASE_Q_COLUMN =9;
	private final int RETURN_Q_COLUMN = 10;
	private final int NET_Q_COLUMN = 11;
	private final int PURCHASE_PRICE_COLUMN = 12;
	private final int PURCHASE_AMOUNT_COLUMN = 13;
	
	private ChainStore chainStore;
	private boolean showCost;
	private Date startDate = new Date();
	private Date endDate = new Date();


    public ChainPurchaseStatisticsDetailReportTemplate(File file) throws IOException{
    	super(file);
    }
	
	public ChainPurchaseStatisticsDetailReportTemplate(List<ChainPurchaseStatisReportItem> items, ChainStore chainStore, String templateWorkbookPath, boolean showCost, Date startDate, Date endDate) throws Exception{
		super(templateWorkbookPath + "\\" + TEMPLATE_NAME);	
		this.items = items;
		this.chainStore = chainStore;
		this.showCost = showCost;

		this.startDate = startDate;
		this.endDate = endDate;

	}
	
	/**
	 * @return
	 */
	public XSSFWorkbook process(){
		XSSFSheet sheet = templateWorkbook.getSheetAt(0);
		//write header
		XSSFRow header1 = sheet.getRow(1);
		header1.createCell(1).setCellValue(Common_util.dateFormat.format(startDate));
		header1.createCell(4).setCellValue(Common_util.dateFormat.format(endDate));
		
		XSSFRow header2 = sheet.getRow(2);
		header2.createCell(1).setCellValue(chainStore.getChain_name());

		
		//write product infmration
		int totalDataRow = items.size();

		for (int i = 0; i < totalDataRow; i++){

			ChainPurchaseStatisReportItem levelFourItem = items.get(i);
			XSSFRow row = sheet.createRow(data_row + i);

			ProductBarcode pb = levelFourItem.getProductBarcode();
			Product product = pb.getProduct();
			
			row.createCell(DATE_COLUMN).setCellValue(levelFourItem.getDate());
			row.createCell(CHAIN_COLUMN).setCellValue(levelFourItem.getChainStore().getChain_name());
			row.createCell(BARCODE_COLUMN).setCellValue(pb.getBarcode());
			row.createCell(PRODUCT_CODE_COLUMN).setCellValue(product.getProductCode());
			Color color = levelFourItem.getProductBarcode().getColor();
			if (color == null)
				row.createCell(COLOR_COLUMN).setCellValue("");
			else 
				row.createCell(COLOR_COLUMN).setCellValue(color.getName());
			
			row.createCell(BRAND_COLUMN).setCellValue(product.getBrand().getBrand_Name());
			
			row.createCell(QUARTER_COLUMN).setCellValue(product.getYear().getYear() + "-" + product.getQuarter().getQuarter_Name());

			row.createCell(CATEGORY_LEVEL_COLUMN).setCellValue(product.getCategoryLevel1().getCategory_Name());
			row.createCell(CATEGORY_COLUMN).setCellValue(product.getCategory().getCategory_Name());
			row.createCell(PURCHASE_Q_COLUMN).setCellValue(levelFourItem.getPurchaseQuantity());
			row.createCell(RETURN_Q_COLUMN).setCellValue(levelFourItem.getReturnQuantity());
			row.createCell(NET_Q_COLUMN).setCellValue(levelFourItem.getNetQuantity());

			if (showCost){
				row.createCell(PURCHASE_PRICE_COLUMN).setCellValue(levelFourItem.getAvgPrice());
				row.createCell(PURCHASE_AMOUNT_COLUMN).setCellValue(levelFourItem.getPurchaseTotalAmt());
			}
		}
	
		return templateWorkbook;
	}
	

}
